<template>
  <el-dialog title="添加房产" :visible="showDialog" @close="cancel">
    <el-form ref="form" :model="DialogForm">
      <el-form-item label="经纪" label-width="80px">
        <el-select v-model="DialogForm.agent" placeholder="请选择">
          <el-option
            v-for="item in agents"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分类" label-width="80px">
        <el-select v-model="DialogForm.category" placeholder="请选择">
          <el-option
            v-for="item in categoreies"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" label-width="80px">
        <el-input v-model="DialogForm.title" />
      </el-form-item>
      <el-form-item label="标签" label-width="80px">
        <el-tag
          v-for="tag in DialogForm.tags"
          :key="tag"
          closable
          @close="handleClose(tag)"
        >
          {{ tag }}
        </el-tag>
        <el-input
          v-if="inputVisible"
          ref="saveTagInput"
          v-model="inputValue"
          class="input-new-tag"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        />
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
      </el-form-item>
      <el-form-item label="地址" label-width="80px">
        <el-input v-model="DialogForm.address" />
      </el-form-item>
      <el-form-item label="价格" label-width="80px">
        <el-input v-model="DialogForm.price" />
      </el-form-item>
      <el-form-item label="描述" label-width="80px">
        <el-input v-model="DialogForm.desc" type="textarea" :rows="2" />
      </el-form-item>
      <el-form-item label="内容" label-width="80px">
        <VueEditor v-model="DialogForm.content" />
      </el-form-item>
      <el-form-item label="介绍图片" label-width="80px">
        <upload ref="photo" :limit="3" />
      </el-form-item>
      <el-form-item label="封面" label-width="80px">
        <upload ref="image" :limit="1" />
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button type="primary" size="small" @click="confirm">确定</el-button>
      <el-button size="small" @click="cancel">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addProperty, editProperty } from '@/api/property'
// 富文本编辑器
import { VueEditor } from 'vue2-editor'

export default {
  components: {
    VueEditor
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      DialogForm: {
        title: '',
        tags: [],
        address: '',
        price: '',
        image: '',
        photos: [],
        desc: '',
        content: '',
        agent: '',
        category: ''
      },
      agents: [],
      categoreies: [],
      inputVisible: false,
      inputValue: ''
    }
  },
  methods: {
    async confirm() {
      // 判断封面是否存在
      if (!this.$refs.image.fileList[0]) {
        this.$message.warning('请先上传封面')
        return
      }
      const obj = {
        ...this.DialogForm,
        // 封面
        image: this.$refs.image.fileList[0].url,
        // 图片修改成数组地址格式
        photos: this.$refs.photo.fileList.map(item => item.url)
      }
      console.log(obj)
      if (this.DialogForm.id) {
        await editProperty(obj)
        this.$message.success('修改成功')
      } else {
        await addProperty(obj)
        this.$message.success('添加成功')
      }
      this.$emit('reload')
      this.$emit('close')
    },
    cancel() {
      this.$emit('close')
      this.$refs.image.fileList = []
      this.$refs.photo.fileList = []
      this.DialogForm = {
        title: '',
        tags: [],
        address: '',
        price: '',
        image: '',
        photos: [],
        desc: '',
        content: '',
        agent: '',
        category: ''
      }
    },
    // 弹框标签显示
    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    // 确认生成标签
    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        this.DialogForm.tags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    },
    // 删除标签
    handleClose(tag) {
      this.DialogForm.tags.splice(this.DialogForm.tags.indexOf(tag), 1)
    }

  }
}

</script>

<style scoped>
::v-deep .el-textarea__inner{
    resize: none;
    width: 100%;
    height: 100px;
}
</style>
